//判断当前有没选中行
function getSelectedNo(info) {
	var object = null;
	$("#providerList>tbody").find("tr").each(function() {
		if ($(this).hasClass('selected')) {
			if (info != "delete") {
				object = $(this).children("td").eq(3).text();
				if (object == "没有数据")
					object = null;
			} else {
				object = $(this);
			}
		}
	});
	return object;
}

// 查看
lookProvider = function() {
	var supplier_enterprise_no = getSelectedNoByForm("#providerList");
	if (supplier_enterprise_no == "" || supplier_enterprise_no == null) {
		$("#look").removeAttr("data-target");
		return;
	}
	$("#look").attr("data-target", "#select");
	$.ajax({
		type : "POST",
		url : "/Medical_Instrument/firstEnterprise/getFirstEnterpriseByNo",
		data : {
			"supplier_enterprise_no" : supplier_enterprise_no
		},
		dataType : "json",
		success : function(data) {
			if (data.result != "failed") {
				$("#providerInfoForm").populateForm(formatDate(data.result));
			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
			;
		},
		error : function(data) {
			layer.msg('请求失败!', {
				icon : 5,
				time : 1000
			});
		}
	});

	

	$("#supplierPeople_table").dataTable().fnDestroy();
	$("#supplierPeople_table").dataTable(
			{
				"processing" : true,
				"serverSide" : false,
				"paginationType" : "full_numbers",
				"ajax" : {
					"type":"POST",
					"url":"/Medical_Instrument/supplierPeople/getSupplierPeopleListById",
					"data":{"supplierNo" : supplier_enterprise_no}
				},
				"aoColumnDefs" : [ {
					"orderable" : false,
					"aTargets" : [ 1, 2, 3, 4, 5, 6, 7 ]
				} ],
				"columns" : [ {
					"data" : "supplierPeopleId",
					"defaultContent" : ""
				}, {
					"data" : "name",
					"defaultContent" : ""
				}, {
					"data" : "duty",
					"defaultContent" : ""
				}, {
					"data" : "identityCard",
					"defaultContent" : ""
				}, {
					"data" : "area",
					"defaultContent" : ""
				}, {
					"data" : "accreditStartDate",
					"defaultContent" : ""
				}, {
					"data" : "accreditEndDate",
					"defaultContent" : ""
				}, {
					"data" : "peopleRemarks",
					"defaultContent" : ""
				}]
			});
}

$(function() {
	$("#providerList").dataTable().fnDestroy();
	$("#providerList").dataTable(
			{
				"processing" : true,
				"serverSide" : false,
				"paginationType" : "full_numbers",
				"ajax" : "/Medical_Instrument/firstEnterprise/getProviderList",
				"aaSorting" : [ [ 0, "desc" ] ], // 默认第几个排序
				"aoColumnDefs" : [ {
					"orderable" : false,
					"aTargets" : [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13,
							14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26,
							27 ]
				} ],
				"columns" : [ {
					"data" : "supplierEnterpriseNo",
					"defaultContent" : ""
				}, {
					"data" : "enterpriseName",
					"defaultContent" : ""
				}, {
					"data" : "inputCode",
					"defaultContent" : ""
				}, {
					"data" : "registerAddress",
					"defaultContent" : ""
				}, {
					"data" : "linkman",
					"defaultContent" : ""
				}, {
					"data" : "linkphone",
					"defaultContent" : ""
				}, {
					"data" : "isAudit",
					"defaultContent" : ""
				}, {
					"data" : "payAmount",
					"defaultContent" : ""
				}, {
					"data" : "incomeAmount",
					"defaultContent" : ""
				}, {
					"data" : "advancePayment",
					"defaultContent" : ""
				}, {
					"data" : "bankAccount",
					"defaultContent" : ""
				}, {
					"data" : "dutyNo",
					"defaultContent" : "",
				}, {
					"data" : "enterprisePhone",
					"defaultContent" : "",
				}, {
					"data" : "postcode",
					"defaultContent" : ""
				}, {
					"data" : "legalPerson",
					"defaultContent" : "",
				}, {
					"data" : "bank",
					"defaultContent" : ""
				}, {
					"data" : "licenseNo",
					"defaultContent" : ""
				}, {
					"data" : "socialCreditCode",
					"defaultContent" : ""
				}, {
					"data" : "qualitySituation",
					"defaultContent" : ""
				}, {
					"data" : "qualityDirecter",
					"defaultContent" : ""
				}, {
					"data" : "ahrqPeopleCount",
					"defaultContent" : ""
				}, {
					"data" : "nhrqContactWay",
					"defaultContent" : ""
				}, {
					"data" : "certificateEndDate",
					"defaultContent" : "",
					"render" : function(data, type, full, meta) {
						return moment(data).format("YYYY-MM-DD");
					}
				}, {
					"data" : "businessEndDate",
					"defaultContent" : "",
					"render" : function(data, type, full, meta) {
						return moment(data).format("YYYY-MM-DD");
					}
				}, {
					"data" : "entrustLimitedDate",
					"defaultContent" : "",
					"render" : function(data, type, full, meta) {
						return moment(data).format("YYYY-MM-DD");
					}
				}, {
					"data" : "qualityEndDate",
					"defaultContent" : "",
					"render" : function(data, type, full, meta) {
						return moment(data).format("YYYY-MM-DD");
					}
				}, {
					"data" : "varietyControl",
					"defaultContent" : ""
				}, {
					"data" : "recordsNo",
					"defaultContent" : ""
				} ]
			});

	$('.cont_right').scroll(function() {
		var obj = $(this);
		var value = obj.scrollLeft();

		$('.dataTables_info').css('margin-left', (value + 'px'));
		$('.dataTables_length').css('margin-left', (value + 'px'));

		$('#providerList_paginate').css('margin-right', ('-' + value + 'px'));
		$('.dataTables_filter').css('margin-right', ('-' + value + 'px'));
	});

});

uploadPic = function() {
	var no = getSelectedNoByForm("#providerList");
	if (no == "" || no == null) {
		return;
	}

	$.ajax({
		type : "post",
		url : "/Medical_Instrument/upload/saveEnterpriseNo",
		data : {
			"enterpriseNo" : no
		},
		dataType : "json",
		success : function(data) {
			if (data.result == "success") {
				picture_add('上传营业执照',
						'/Medical_Instrument/page/gsp/picture-add');

			} else {
				layer.msg('请求失败!', {
					icon : 5,
					time : 1000
				});
			}
		},
		error : function(data) {
			layer.msg('发生错误,请稍后再试!', {
				icon : 5,
				time : 1000
			});
		}
	});

}

/* 上传营业执照页面 */
function picture_add(title, url) {
	var index = layer.open({
		type : 2,
		title : title,
		content : url
	});
	layer.full(index);
}

// 根据表格id判断当前有没选中行
function getSelectedNoByForm(formId) {
	var object = null;
	$(formId).find("tr").each(function() {
		if ($(this).hasClass('selected')) {
			if (formId != "delete") {
				object = $(this).children("td").eq(0).text();
				if (object == "没有数据")
					object = null;
			} else {
				object = $(this);
			}
		}
	});
	return object;
}

$.fn.populateForm = function(data) {
	return this.each(function() {
		var formElem, name;
		if (data == null) {
			this.reset();
			return;
		}
		for ( var i = 0; i < this.length; i++) {
			formElem = this.elements[i];
			// checkbox的name可能是name[]数组形式
			name = (formElem.type == "checkbox") ? formElem.name.replace(
					/(.+)\[\]$/, "$1") : formElem.name;
			if (data[name] == undefined)
				continue;
			switch (formElem.type) {
			case "checkbox":
				if (data[name] == "") {
					formElem.checked = false;
				} else if (data[name] == "1" || data[name] == 1) {
					formElem.checked = true;
				} else {
					// 数查找元素
					if (data[name].indexOf(formElem.value) > -1) {
						formElem.checked = true;
					} else {
						formElem.checked = false;
					}
				}
				break;
			case "radio":
				if (data[name] == "") {
					formElem.checked = false;
				} else if (formElem.value == data[name]) {
					formElem.checked = true;
				}
				break;
			case "button":
				break;
			default:
				formElem.value = data[name];
			}
		}
	});
};

// 处理日期格式
function formatDate(data) {
	var enterprise = eval(data);
	if (enterprise.varietyControl == 1) {
		$("#isVarietyControl").attr("checked", "checked");
	} else {
		$("#isVarietyControl").attr("checked", false);
	}
	if (enterprise.sellColdItem == 1) {
		$("#isSellColdItem").attr("checked", "checked");
	} else {
		$("#isSellColdItem").attr("checked", false);
	}
	var date = new Date();
	if (enterprise.entrustLimitedDate != ""
			&& enterprise.entrustLimitedDate != null) {
		date = new Date(enterprise.entrustLimitedDate);
		enterprise.entrustLimitedDate = date.format("yyyy-MM-dd");
	}
	if (enterprise.businessEndDate != "" && enterprise.businessEndDate != null) {
		date = new Date(enterprise.businessEndDate);
		enterprise.businessEndDate = date.format("yyyy-MM-dd");

	}

	if (enterprise.inputDate != "" && enterprise.inputDate != null) {
		date = new Date(enterprise.inputDate);
		enterprise.inputDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.organizationLimitedDate != ""
			&& enterprise.organizationLimitedDate != null) {
		date = new Date(enterprise.organizationLimitedDate);
		enterprise.organizationLimitedDate = date.format("yyyy-MM-dd");
	}
	if (enterprise.qualityStartDate != ""
			&& enterprise.qualityStartDate != null) {
		date = new Date(enterprise.qualityStartDate);
		enterprise.qualityStartDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.qualityEndDate != "" && enterprise.qualityEndDate != null) {
		date = new Date(enterprise.qualityEndDate);
		enterprise.qualityEndDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.certificateStartDate != ""
			&& enterprise.certificateStartDate != null) {
		date = new Date(enterprise.certificateStartDate);
		enterprise.certificateStartDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.certificateEndDate != ""
			&& enterprise.certificateEndDate != null) {
		date = new Date(enterprise.certificateEndDate);
		enterprise.certificateEndDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.businessStartDate != ""
			&& enterprise.businessStartDate != null) {
		date = new Date(enterprise.businessStartDate);
		enterprise.businessStartDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.businessEndDate != "" && enterprise.businessEndDate != null) {
		date = new Date(enterprise.businessEndDate);
		enterprise.businessEndDate = date.format("yyyy-MM-dd");
	}
	if (enterprise.qualityPeopleConclusionDate != ""
			&& enterprise.qualityPeopleConclusionDate != null) {
		date = new Date(enterprise.qualityPeopleConclusionDate);
		enterprise.qualityPeopleConclusionDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.businessAuditDate != ""
			&& enterprise.businessAuditDate != null) {
		date = new Date(enterprise.businessAuditDate);
		enterprise.businessAuditDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.qualityAdminDate != ""
			&& enterprise.qualityAdminDate != null) {
		date = new Date(enterprise.qualityAdminDate);
		enterprise.qualityAdminDate = date.format("yyyy-MM-dd");
	}

	if (enterprise.qualityDeptDate != "" && enterprise.qualityDeptDate != null) {
		date = new Date(enterprise.qualityDeptDate);
		enterprise.qualityDeptDate = date.format("yyyy-MM-dd");
	}
	if (enterprise.qualityPeopleOpinionDate != ""
			&& enterprise.qualityPeopleOpinionDate != null) {
		date = new Date(enterprise.qualityPeopleOpinionDate);
		enterprise.qualityPeopleOpinionDate = date.format("yyyy-MM-dd");

	}
	return enterprise;
}
Date.prototype.format = function(format) {
	var o = {
		"M+" : this.getMonth() + 1, // month
		"d+" : this.getDate(), // day
		"h+" : this.getHours(), // hour
		"m+" : this.getMinutes(), // minute
		"s+" : this.getSeconds(), // second
		"q+" : Math.floor((this.getMonth() + 3) / 3), // quarter
		"S" : this.getMilliseconds()
	// millisecond
	};
	if (/(y+)/.test(format))
		format = format.replace(RegExp.$1, (this.getFullYear() + "")
				.substr(4 - RegExp.$1.length));
	for ( var k in o)
		if (new RegExp("(" + k + ")").test(format))
			format = format.replace(RegExp.$1, RegExp.$1.length == 1 ? o[k]
					: ("00" + o[k]).substr(("" + o[k]).length));
	return format;
};